//package cn.iocoder.yudao.module.handguard.tool.http;//package cn.iocoder.yudao.module.handguard.tool;
//
//import cn.iocoder.yudao.module.handguard.tool.Base64ToFile;
//import cn.iocoder.yudao.module.handguard.tool.MD5Util;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import net.sourceforge.tess4j.ITesseract;
//import net.sourceforge.tess4j.Tesseract;
//import net.sourceforge.tess4j.TesseractException;
//import org.apache.http.client.methods.CloseableHttpResponse;
//import org.apache.http.client.methods.HttpGet;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.util.EntityUtils;
//import org.apache.commons.lang3.RandomStringUtils;
//import org.opencv.core.Mat;
//import org.opencv.imgcodecs.Imgcodecs;
//
//import java.awt.Color;
//import java.awt.image.BufferedImage;
//import javax.imageio.ImageIO;
//import java.io.File;
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.List;
//
//public class CaptchaRecognizer {
//
//    public static void main(String[] args) throws IOException {
//
//        String detailsUrl = "https://cq.ke.com/xiaoqu/rs"; // 更换为实际的详情接口URL
////
////
//        String captchaUrl = "https://captcha.lianjia.com/captcha/resource?sceneId=sec-hip&token="; // 更换为实际的验证码图片URL
//        // 获取验证码图片
//        String randomString = RandomStringUtils.randomAlphanumeric(10);
//        String token = MD5Util.MD5Encode(randomString, "utf-8");
//        // 生成一个包含字母和数字的长度为10的随机字符串
//        captchaUrl += token+"&callback=__jp1";
//        String captchaImage = getCaptchaImage(captchaUrl);
//
//        String captchaText = "";
//        captchaImage = captchaImage.replace("/**/__jp1(","");
//        captchaImage = captchaImage.replace(");","");
//        JSONObject captchaJson = JSON.parseObject(captchaImage);
//        JSONObject data = JSON.parseObject(captchaJson.get("data").toString());
//        JSONObject puzzle = JSON.parseObject(data.get("puzzle").toString());
//        String resource = puzzle.get("resource").toString();
//        resource = resource.replace("data:image/jpeg;base64, ","");
//        String fileImgPath = "/Users/handsome/LDFC/img/"+token+".jpeg";
//
//        File file = Base64ToFile.base64ToFile(resource,fileImgPath);
//
//        // 步骤1：加载图片
////        BufferedImage image = null;
////        try {
////            image = loadImage(fileImgPath);
////        } catch (IOException e) {
////            throw new RuntimeException(e);
////        }
//
////        // 步骤2：处理图片
//       // BufferedImage processedImage = processImage(image);
////        Tesseract tesseract = new Tesseract();
////        // 仅识别数字
////        //tesseract.setTessVariable("tessedit_char_whitelist", "0123456789");
////        tesseract.setDatapath("/opt/homebrew/Cellar/tesseract/5.4.1/share/tessdata"); // 设置Tesseract的训练数据目录
////
////        String path = "/Users/handsome/LDFC/img/83d18214743377f29734d9f5a7c3eb3c.jpeg";
////
////        BufferedImage originalImage = ImageIO.read(new File(path));
////        BufferedImage grayImage = ImageProcessorAWT.grayScale(originalImage);
////        BufferedImage binaryImage = ImageProcessorAWT.binaryThreshold(grayImage, 220); // 阈值可以根据实际情况调整
////
////
////
////        try {
////            captchaText = tesseract.doOCR(grayImage);
////        } catch (TesseractException e) {
////            throw new RuntimeException(e);
////        }
////
////        // 步骤4：输出结果
////        System.out.println("识别到的验证码是: " +  captchaText);
//
////        recognizeText(null);
//    }
//
//    private static String getCaptchaImage(String url) {
//        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
//            HttpGet httpGet = new HttpGet(url);
//            CloseableHttpResponse response = httpClient.execute(httpGet);
//            return EntityUtils.toString(response.getEntity(), "UTF-8");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    public static BufferedImage loadImage(String filePath) throws IOException {
//        // 读取本地文件中的验证码图片
//        File file = new File(filePath);
//        BufferedImage image = ImageIO.read(file);
//        return image;
//    }
//
//    public static BufferedImage processImage(BufferedImage image) {
//        // 创建一个新的空白图片
//        BufferedImage processedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
//
//        for (int x = 0; x < image.getWidth(); x++) {
//            for (int y = 0; y < image.getHeight(); y++) {
//                // 获取原始图片的颜色
//                Color c = new Color(image.getRGB(x, y));
//                // 获取灰度值
//                int gray = (int) (c.getRed() * 0.2989 + c.getGreen() * 0.5870 + c.getBlue() * 0.1140);
//                // 将灰度值转换为黑白
//                processedImage.setRGB(x, y, gray < 128 ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
//            }
//        }
//        return processedImage;
//    }
//
//
//    public static String recognizeText(BufferedImage image) {
//        Tesseract tesseract = new Tesseract();
//        tesseract.setDatapath("/opt/homebrew/Cellar/tesseract/5.4.1/share/tessdata"); // 设置Tesseract的训练数据目录
//        tesseract.setLanguage("eng"); // 设置语言，中文为"chi_sim"
//
//        try {
//            // 识别图片中的文字
//            return tesseract.doOCR(image);
//        } catch (TesseractException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
//
//
//
//
//
//    private static String sendDetailsRequest(String url, String captcha) {
//       return "";
//    }
//}